#===============================================================================
# Koro Business Environment
# Copyright (C) 2008  Antonio Corroppoli
#
#This file is part of Koro BE.
# 
# Koro BE is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Koro BE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Koro BE; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#===============================================================================

'''
Created on 11/ago/2011

@author: antstud
'''

from koro.db import database
from koro.core.context import Context
from koro.modules.home import model

#===============================================================================
# HomeContext
#===============================================================================
class HomeContext(Context):
    
    def __init__(self):
        Context.__init__(self)
        print 'home context'
    
#    def get_memo(self,*args,**kwargs):
#        print 'ooooooooooooooooooooooooo'
#        self._context=Agenda
#        return self.get_objects(*args,**kwargs)
#
#    def render(self):
#        print 'Context.render'   
#        view.get_instance().show_home_page()
    
    def get_object_class(self):
        return self._context
    
    def get_objects(self,*args,**kwargs):
        if self._context is None:
            return []
        query=database.get_instance().query(self._context)
        if len(kwargs)>0:
            query=query.filter_by(**kwargs)
        if len(args)>0:
            query=query.filter(*args)
        return query.all()
    
    def save_object(self,obj):
        database.get_instance().save(obj)
    
    def delete_object(self,obj):
        database.get_instance().delete(obj)
    
    def new_object(self):
        raise Exception('Metodo non disponibile.')
    
    def update_object(self,obj):
        self.save_object(obj)
    

#===============================================================================
# LavagnaContext
#===============================================================================
class LavagnaContext(Context):
    def __init__(self):
        Context.__init__(self,model.Lavagna)
    
    def render(self):
        pass

    def get_objects(self,*args,**kwargs):
        query=database.get_instance().query(self._context).filter_by(ID=0)
        return query.one()
    
    def save_object(self,obj):
        obj.ID=0
        database.get_instance().save(obj)